//
//  Person.m
//  OC07_内存管理
//
//  Created by Scott on 16/3/4.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "Person.h"

@implementation Person

/** 实现了setter, getter方法. 在Xcode 4.x版本之后, 此行可以省略了.
 * 注意: 但是如果同时实现setter, getter方法时, 会提示_属性名 不识别. 此时加上这个代码即可.
 */
@synthesize sex = _sex;
@synthesize age = _age;
@synthesize name = _name;


#pragma mark Creating and Init Person
- (instancetype)initWithName:(NSString *)name sex:(NSString *)sex age:(NSInteger)age {
    
    self = [super init];
    if (self) {
        
        self.name = name;
        self.sex = sex;
        self.age = age;
    }
    
    return self;
}

+ (instancetype)personWithName:(NSString *)name sex:(NSString *)sex age:(NSInteger)age {
    
    return [[[Person alloc] initWithName:name sex:sex age:age] autorelease];
}




#pragma mark - overwrite
- (void)dealloc {
    
    NSLog(@"person对象马上就清空了");
    
    [_name release];
    [_sex release];

    [super dealloc]; /**< 最后一句必须调用父类的dealloc方法,完成内存释放. */
}


#pragma mark - setter, getter
/** retain 语意设置的属性setter, getter方法实现. */
- (void)setSex:(NSString *)sex {
    
    if (_sex != sex) {
        
        [_sex release];
        
        _sex = [sex retain];
    }
}

- (NSString *)sex {
    return [[_sex retain] autorelease];
}

/** assgin 语意设置的属性 setter, getter方法实现. */
- (void)setAge:(NSInteger)age {
    _age = age;
}

- (NSInteger)age {
    return _age;
}

/** copy 语意设置的属性 setter, getter方法实现. */
- (void)setName:(NSString *)name {
    
    if (_name != name) {
        [_name release];
        _name = [name copy];
    }
}

- (NSString *)name {
    return [[_name retain] autorelease];
}


#pragma mark - 实现NSCopying协议方法
- (id)copyWithZone:(NSZone *)zone {
    
    // 实现对象的拷贝.
    Person *person = [[Person allocWithZone:zone] init];
    
    person.name = self.name;
    person.sex = self.sex;
    person.age = self.age;
    
    return person;
}









@end
